﻿using B.S.DataBase.MyProject.Domain.BOM;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.BomCOmmand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.BomConnamdHandler
{
    /// <summary>
    /// 查询Bom信息
    /// 物联网-智能MES生产管理系统-01-BOM管理V1.1-20250206
    /// </summary>
    public class GetBomCommandHandler : IRequestHandler<GetBomCommand, APIResult<List<BomInfo>>>
    {
        private readonly IBaseRepository<BomInfo> bomRep;
        private readonly IBaseRepository<OperationRecord> operatRep;
        public GetBomCommandHandler(IBaseRepository<BomInfo> bomRep, IBaseRepository<OperationRecord> operatRep)
        {
            this.bomRep = bomRep;
            this.operatRep = operatRep;
        }
        public Task<APIResult<List<BomInfo>>> Handle(GetBomCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<BomInfo>> result=new APIResult<List<BomInfo>>();
            var list= bomRep.GetAll().Result;
            if (!string.IsNullOrEmpty(request.BomCode))
            {
                list = list.Where(x => x.BomCode == request.BomCode);
            }
            if (!string.IsNullOrEmpty(request.ProductName))
            {
                list = list.Where(x => x.ProductName.Contains(request.ProductName));
            }
            OperationRecord operation = new OperationRecord();
            list = list.OrderByDescending(x => x.BId);
            operation.OperationName = "张龙";
            operation.OperationDate = DateTime.Now;
            operation.OperationInfo = "列表查询";
            operatRep.Add(operation);
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
